<template>
  <div class="container">
    <div class="login">
      <img src="../assets/logo.png" class="login-img" alt="" />
      <!-- 登录 -->
      <el-form
        :model="loginForm"
        :rules="rules"
        ref="loginForm"
        v-if="!loginStatus"
      >
        <el-row :gutter="24" class="login-msg" style="marign: 0">
          <el-col :span="16" :offset="4" style="padding: 0">
            <el-form-item prop="name">
              <el-input
                prefix-icon="iconfont iconicon"
                v-model="loginForm.name"
                placeholder="用户名"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24" class="login-msg2" style="marign: 0">
          <el-col :span="16" :offset="4" style="padding: 0">
            <el-form-item prop="password">
              <el-input
                prefix-icon="iconfont iconmima"
                v-model="loginForm.pw"
                placeholder="密码"
                type="password"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <div class="login-btn">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button @click="toSing">注册</el-button>
        </div>
      </el-form>
      <!-- 注册 -->
      <el-form
        :model="singFrom"
        :rules="rules"
        ref="refSing"
        v-if="loginStatus"
      >
        <el-row :gutter="24" class="login-msg" style="marign: 0">
          <el-col :span="16" :offset="4" style="padding: 0">
            <el-form-item prop="name">
              <el-input
                prefix-icon="iconfont iconicon"
                v-model="singFrom.name"
                placeholder="用户名"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24" class="login-msg2" style="marign: 0">
          <el-col :span="16" :offset="4" style="padding: 0">
            <el-form-item prop="password">
              <el-input
                prefix-icon="iconfont iconmima"
                v-model="singFrom.pw"
                placeholder="密码"
                type="password"
              ></el-input>
            </el-form-item>
            <!-- <el-input placeholder="密码"></el-input> -->
          </el-col>
        </el-row>
        <div class="login-btn">
          <el-button style="width: 50%" @click="sing">注册</el-button>
          <a href="javascript:;" @click="resLogin">返回登录?</a>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import md5 from "js-md5";
export default {
  data() {
    return {
      loginStatus: false,

      loginForm: {
        name: "",
        pw: ""
      },
      singFrom: {
        name: "",
        pw: ""
      },
      rules: {
        name: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" }
        ],
        pw: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" }
        ]
      }
    };
  },
  mounted() {
    // console.log(md5("hellow") + "md5");
  },
  methods: {
    //登录
    login() {
      this.$refs.loginForm.validate(async valid => {
        if (!valid) return;
        let pw = md5(this.loginForm.pw) + "md5";
        const { data: res } = await this.$http.post("api/admin/login", {
          name: this.loginForm.name,
          pw: pw
        });
        // console.log(data)
        if (res.code !== 2000) return this.$message.error("登录失败");
        window.sessionStorage.setItem("token", res.data.token);
        window.sessionStorage.setItem("user", res.data.admin_name);
        this.$message.success("登录成功");
        if (!window.sessionStorage.getItem("activePath")) {
          this.$router.push("/index");
        } else {
          this.$router.push(window.sessionStorage.getItem("activePath"));
        }
      });
    },
    //点击进入登录页面
    toSing() {
      this.$refs.loginForm.resetFields();
      this.loginStatus = true;
    },
    //注册
    sing() {
      this.$refs.refSing.validate(async valid => {
        if (!valid) return;
        // console.log(this.singFrom)
        let pw = md5(this.singFrom.pw) + "md5";
        const { data: res } = await this.$http.post("api/admin/register", {
          name: this.singFrom.name,
          pw: pw
        });
        // console.log(res)
        if (res.code !== 2000)
          return this.$message.error("用户已经存在,请重新输入!");
        this.$message.success("注册成功");

        const { data: resSing } = await this.$http.post("api/admin/login", {
          name: this.singFrom.name,
          pw: pw
        });

        if (resSing.code !== 2000) return this.$message.error("登录失败");
        window.sessionStorage.setItem("token", resSing.data.token);
        window.sessionStorage.setItem("user", resSing.data.admin_name);
        this.loginStatus = false;
        this.$refs.refSing.resetFields();
        // this.$router.push("/index");
        if (!window.sessionStorage.getItem("activePath")) {
          this.$router.push("/index");
        } else {
          this.$router.push(window.sessionStorage.getItem("activePath"));
        }
      });
    },
    resLogin() {
      // console.log(this.loginStatus)
      this.loginStatus = false;
    }
  }
};
</script>
<style lang="less" scoped>
.login-btn {
  a {
    font-size: 12px;
    position: absolute;
    right: 20px;
    bottom: 0;
    color: #333;

    &:hover {
      cursor: pointer;
      color: #4390ee;
    }
  }
}
.container {
  width: 100%;
  height: 100%;
  background: #005858;
  position: relative;
  background: url(../../static/homtopbg.png) center center no-repeat;
  background-size: cover;

  .login {
    width: 450px;
    height: 350px;
    background: #fff;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    border-radius: 10px;
    .login-img {
      width: 80px;
      height: 80px;
      border-radius: 50%;
      position: absolute;
      left: 40%;
      top: 10%;
      background: #f6f6f6;
    }
    .login-btn {
      width: 100%;
      position: absolute;
      top: 80%;
      display: flex;
      justify-content: center;
      align-items: center;
      // .el-button {
      //   width: 50%;
      // }
    }
    .login-msg2 {
      width: 100%;
      position: absolute;
      top: 58%;
    }
    .login-msg {
      width: 100%;
      position: absolute;
      top: 40%;

      .el-input__inner {
        border-radius: 0;
      }
    }
  }
}
</style>
